package fz.vrd.app;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.activity.ComponentActivity;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import fz.vrd.library.page.activity.BaseAc;
import fz.vrd.library.permissions.RequestPermissionsCallback;

public class PermissionsAc extends BaseAc {

    TextView tv_info;


    String camera = Manifest.permission.CAMERA;
//    String camera = "android.permission.WRITE_EXTERNAL_STORAGE";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permissions);
        findViewById(R.id.btn_get).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                request();
            }
        });
        findViewById(R.id.btn_check).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isTrue = isHas();
                addInfo(isTrue ? "权限已授权" : "权限未授权");
                boolean dd = page.getPermissionsManage().shouldShowRequestPermissionRationale(camera);
                addInfo(dd ? "有请求过权限" : "从来没有请求过权限");
            }
        });
        findViewById(R.id.ben_clear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_info.setText("信息如下：\n");
            }
        });
        tv_info = findViewById(R.id.tv_info);
        tv_info.setText("信息如下：\n");
        page.getPermissionsManage().setCallBack(callBack);
    }

    void addInfo(String info) {
        tv_info.append(info + "\n");
    }

    void request() {
        addInfo("开始请求权限");

        page.getPermissionsManage().setPermissions(new String[]{camera}).requestPermissions();
    }

    RequestPermissionsCallback callBack = new RequestPermissionsCallback() {

        @Override
        public void success(String[] permissions) {
            addInfo("权限申请成功了");
        }

        @Override
        public void fail(boolean isShow, String[] permissions) {
            addInfo("功能需要这个权限了" + isShow);
        }
    };

    /**
     * true: PackageManager.PERMISSION_GRANTED
     * fale: PackageManager.PERMISSION_DENIED
     */
    boolean isHas() {
        return page.getPermissionsManage().hasPermission(camera);
//        return ContextCompat.checkSelfPermission(this, camera) == PackageManager.PERMISSION_GRANTED;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100) {
            if (grantResults.length > 0) {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    addInfo("授权成功");
                } else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
                    addInfo("授权拒绝,不在弹框提示");
                }
            } else {
                addInfo("授权拒绝");
            }
        }
    }

    private ActivityResultLauncher<String> requestPermissionLauncher =
            registerForActivityResult(new ActivityResultContracts.RequestPermission(), new ComponentActivity().getActivityResultRegistry(), new ActivityResultCallback<Boolean>() {
                @Override
                public void onActivityResult(Boolean isGranted) {
                    if (isGranted) {
                        addInfo("授权成功了");
                    } else {
                        addInfo("授权拒绝了");
                    }
                }
            });
}